#!/usr/bin/env python
import cohen_preprocess as pre
import random
import numpy as np
import helper
import sys

#TODO
#Processering af input matricer i komprimeret format
# Hitfactor and threshold
data_dir = '../data'
filename = 'testinput.dat'
Ak = [] # Input list of k matrices
Wij =  [] # list of lists of sums for each row in layer k with outgoing edges to layer k+1
# Only last sum of rows are necessary but keep a list for verbose output
ProbAccum = [] #list of accumulated probabilities for each subproduct

_Helper = helper.Helper()

I,J,V, Keys = _Helper.parse_abc_file(filename, data_dir)
csr_mat = _Helper.build_COO_matrix(I,J,V, len(Keys))
print "csr_mat: \n", str(csr_mat)
print ("nnz: " + str(csr_mat.nnz))
print ("dimen: " + str(csr_mat.ndim))
print ("shape: " + str(csr_mat.shape))

A = [[1,3,2], [1,3,2], [0,3,2]]
B = [[1,3,2], [0,3,2], [1,3,2]]
Wij.append(A)
Wij.append(B)
#Wij.append(pre.process_csr_sum(csr_mat))
Wij.append(pre.preprocess_sum(A))

print "Wij:\n" , str(Wij)
#_wij, pr = pre.compute_csr_Wij(csr_mat, Wij)
_wij, pr = pre.compute_Wij(B, Wij)
Wij.append(_wij)
ProbAccum.append(pr)

ProbAccum.reverse()
S = 3
Li = np.zeros((1, csr_mat.shape[0]), dtype=np.int)
for s in range(S):
	v = random.randint(0,csr_mat.shape[0]) # randomly select starting node / Should this be weighted as well?
	row = None
	for p_matrix in ProbAccum:
		row = p_matrix[v]
		prob_dist = row[0]
		#prob_dist = matrix[v] # select accumulated probabilities for row v of matrix
		v = pre.sample(random.random(), prob_dist) #
	col_idx = row[1]
	#Li[col_idx[v]] += 1 # increment samplings on j 

#print (Li)